package com.micro.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.jsoup.helper.StringUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * POI解析Excel
 */
public class ExcelUtil {

    /**
     * 根据fileType不同读取excel文件
     *
     * @param path
     * @param path
     * @throws IOException
     */
    public static List<List<String>> readExcel(String path) {
        String fileType = path.substring(path.lastIndexOf(".") + 1);
        // return a list contains many list
        List<List<String>> lists = new ArrayList<List<String>>();
        //读取excel文件
        InputStream is = null;
        try {
            is = new FileInputStream(path);
            //获取工作薄
            Workbook wb = null;
            if (fileType.equals("xls")) {
                wb = new HSSFWorkbook(is);
            } else if (fileType.equals("xlsx")) {
//                wb = new XSSFWorkbook(is);
            } else {
                return null;
            }

            //读取第一个工作页sheet
            Sheet sheet = wb.getSheetAt(0);
            //第一行为标题
            for (Row row : sheet) {
                ArrayList<String> list = new ArrayList<String>();
                for (Cell cell : row) {
                    //根据不同类型转化成字符串
//                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    list.add(cell.getStringCellValue());
                }
                lists.add(list);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return lists;
    }

    public static void main(String[] args) throws ParseException {
        String path = "d:/每日健康统计2.xls";
        List<List<String>> lists = readExcel(path);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat fhh = new SimpleDateFormat("HH");
        // 未打卡
        boolean wdk = false;
        // 工作日迟到打卡
        boolean gzrcddk = false;
        // 周末迟到打卡
        boolean zmcddk = false;
        // 未打卡罚款
        Map<String,Integer> wdkfkMap = new HashMap();
        // 迟到罚款
        Map<String,Integer> cdfkMap = new HashMap();
        for (int i = 1; i < lists.size(); i++) {
            List<String> itemCell = lists.get(i);
            String name = "["+itemCell.get(0)+"]"+itemCell.get(1);

            if (name.equals("[0027003224]胡庆云")) {
                continue;
            }
            // 填写时间
            String txsj = itemCell.get(37);
            // 采集时间
            String cjsj = itemCell.get(8);
            String valueStr = name + "    采集时间："+cjsj+"    填写时间：" + txsj;

            Date txsjDate = null;
            if (StringUtil.isBlank(txsj)) {
                if (!wdk) {
                    System.out.println("健康打卡未打卡人员名单：");
                    wdk = true;
                }
                Integer wdkfk = wdkfkMap.get(name);
                if (wdkfk != null) {
                    wdkfk = wdkfk + 200;
                    wdkfkMap.put(name, wdkfk);
                } else {
                    wdkfkMap.put(name, 200);
                }
                System.out.println(valueStr);
            } else {
                txsjDate = sdf.parse(txsj);
                String txsjHH = fhh.format(txsjDate);
                String httpArg = cjsj.replaceAll("-","");
                Integer isHoliday = HolidayUtil.getHolidayData(httpArg);
                // 1 工作日 2周末or节假日
                if (isHoliday != null) {
                    if (isHoliday == 1) {
                        if (Integer.parseInt(txsjHH) >= 9) {
                            if (!gzrcddk) {
                                System.out.println("工作日迟到打卡人员名单：");
                                gzrcddk = true;
                            }
                            Integer cdfk = cdfkMap.get(name);
                            if (cdfk != null) {
                                cdfk = cdfk + 50;
                                cdfkMap.put(name, cdfk);
                            } else {
                                cdfkMap.put(name, 50);
                            }
                            System.out.println(valueStr);
                        }
                    } else {
                        if (Integer.parseInt(txsjHH) >= 12) {
                            if (!zmcddk) {
                                System.out.println("周末迟到打卡人员名单：");
                                zmcddk = true;
                            }
                            Integer cdfk = cdfkMap.get(name);
                            if (cdfk != null) {
                                cdfk = cdfk + 50;
                                cdfkMap.put(name, cdfk);
                            } else {
                                cdfkMap.put(name, 50);
                            }
                            System.out.println(valueStr);
                        }
                    }
                } else {
                    System.out.println("未找到此时间："+cjsj);
                }

            }
        }

        for(Map.Entry<String, Integer> entry : wdkfkMap.entrySet()){
            String mapKey = entry.getKey();
            Integer mapValue = entry.getValue();

            Integer mapValue2 = cdfkMap.get(mapKey);
            if (null != mapValue2) {
                mapValue = mapValue + mapValue2;
                wdkfkMap.put(mapKey, mapValue);
                // 移除相同项
                cdfkMap.remove(mapKey);
            }
        }

        for(Map.Entry<String, Integer> entry2 : cdfkMap.entrySet()){
            String mapKey2 = entry2.getKey();
            Integer mapValue2 = entry2.getValue();

            wdkfkMap.put(mapKey2, mapValue2);
        }

        System.out.println("罚款名单：");
        for(Map.Entry<String, Integer> entry : wdkfkMap.entrySet()){
            String mapKey = entry.getKey();
            Integer mapValue = entry.getValue();

            System.out.println(mapKey+"  :  "+mapValue);
        }
    }
}